package org.jeecg.modules.carRegister.scheduled;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.carRegister.entity.CarRegister;
import org.jeecg.modules.carRegister.service.ICarRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Configuration
@EnableScheduling
@Slf4j
public class MyScheduled {

    @Autowired
    private ICarRegisterService carRegisterService;

    @Scheduled(cron = "0 0 * * * ?")//每小时执行一次
    public void configureTasks() throws Exception{
        LambdaQueryWrapper<CarRegister> wrapper = Wrappers.lambdaQuery();
        wrapper.in(CarRegister::getApprovalStatus, 0, 1);//新登记的, 已通过的
        List<CarRegister> carRegisters = carRegisterService.list(wrapper);
        List<CarRegister> updatedCarRegisterList = new ArrayList<>();
        // 获取当前时间的Instant
        Instant now = Instant.now();
        for (CarRegister carRegister : carRegisters) {
            Date createTime = carRegister.getCreateTime();
            Instant instant = createTime.toInstant();
            // 计算两个时间点之间的小时数
            long hoursBetween = ChronoUnit.HOURS.between(instant, now);
            // 判断是否超过24小时
            if (hoursBetween > 24) {
                carRegister.setApprovalStatus(3);
                updatedCarRegisterList.add(carRegister);
            }
        }
        if (!updatedCarRegisterList.isEmpty()){
            carRegisterService.updateBatchById(updatedCarRegisterList);
        }
    }

}
